package org.test.jiazi_1.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.test.jiazi_1.pojo.entity.Course;
import org.test.jiazi_1.service.CourseService;
import org.test.jiazi_1.service.FitnessService;
import org.test.jiazi_1.service.ScoresService;

import java.util.List;

/**
 * 课程信息表(Course)表控制层
 *
 * @author 2251300222 唐文涛
 * @since 2025-10-24 13:27:53
 */
@Slf4j
@RestController
@RequestMapping("course")
@Tag(name = "课程信息表(Course)表控制层")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;
    @Resource
    private FitnessService fitnessService;
    @Resource
    private ScoresService scoresService;

    /**
     * 全查询
     *
     * @param course 筛选条件
     * @return 查询结果
     */
    @Operation(summary = "全查询")
    @GetMapping
    public List<Course> queryAll(@Parameter(name = "course 筛选条件") Course course) {

        return courseService.list();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "通过主键查询单条数据")
    @GetMapping("{id}")
    public Course queryById(@PathVariable("id") Integer id) {
        return courseService.getById(id);
    }

    /**
     * 新增数据
     *
     * @param course 实体
     * @return 新增结果
     */
    @Operation(summary = "新增数据")
    @PostMapping
    public boolean add(@Parameter(name = "course 实体") @RequestBody Course course) {
        return courseService.save(course);
    }

    /**
     * 编辑数据
     *
     * @param course 实体
     * @return 编辑结果
     */
    @Operation(summary = "编辑数据")
    @PutMapping
    public boolean edit(@Parameter(name = "course 实体") @RequestBody Course course) {
        return courseService.updateById(course);
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @Operation(summary = "删除数据")
    @DeleteMapping
    public boolean deleteById(@Parameter(name = "id 主键") Integer id) {
        return courseService.removeById(id);
    }

    //结束课程
    @Operation(summary = "结束课程")
    @PutMapping("/end/{id}")
    public void endCourse(@PathVariable("id") Integer id) {
        //结束课程
        courseService.endCourse(id);

        //计算成绩
        scoresService.calculateScores(id);

    }

}

